package com.fuchuang.seckillsystem.controller;

import com.fuchuang.seckillsystem.dto.ActivityQuery;
import com.fuchuang.seckillsystem.dto.PageQuery;
import com.fuchuang.seckillsystem.entity.Activity;
import com.fuchuang.seckillsystem.entity.Goods;
import com.fuchuang.seckillsystem.entity.MyUserDetails;
import com.fuchuang.seckillsystem.service.IActivityService;
import com.fuchuang.seckillsystem.vo.ActivityGoodsVo;
import com.fuchuang.seckillsystem.vo.RespBean;
import com.fuchuang.seckillsystem.vo.RespBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("activity")
public class ActivityController {
    
    
    @Autowired
    IActivityService activityService;
    
    
    /**
     * 开始时间 结束时间 活动介绍 活动名称
     */
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    @PostMapping("add")
    public RespBean insertActivity(@RequestBody Activity activity) {
        activityService.insertActivity(activity);
        return RespBean.success();
    }
    
    @PostMapping("addGoods")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean insertActivityGoods(@RequestBody ActivityGoodsVo activityGoodsVo) {
        activityService.insertActivityGoods(activityGoodsVo.getActivityId(), activityGoodsVo.getGoodsIds());
        return RespBean.success();
    }
    
    @PostMapping("deleteGoods")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean deleteActivityGoods(@RequestBody ActivityGoodsVo activityGoodsVo) {
        activityService.deleteActivityGoods(activityGoodsVo.getActivityId(), activityGoodsVo.getGoodsIds());
        return RespBean.success();
    }
    
    @PostMapping("delete")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean deleteActivity(@RequestParam("activityId") Integer activityId) {
        activityService.deleteActivity(activityId);
        return RespBean.success();
    }
    
    @PostMapping("update")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_PRODUCT')")
    public RespBean updateActivity(@RequestBody Activity activity) {
        activityService.updateActivity(activity);
        return RespBean.success();
    }
    
    @PostMapping("selectAll")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_FORM','ROLE_PRODUCT')")
    public RespBean selectAllActivity(@RequestBody PageQuery pageQuery) {
        Map<String, Object> map = activityService.selectAllActivity(pageQuery);
        return RespBean.success(map);
    }
    
    /**
     * 查询指定时间内的活动，展示给用户
     * 默认为过去一天和未来一天内的秒杀活动
     */
    @PostMapping("selectByDate")
    public RespBean selectActivityByDate(@RequestBody ActivityQuery activityQuery) {
        Map<String, Object> map = activityService.selectActivityByDate(activityQuery);
        return RespBean.success(map);
    }
    
    @GetMapping("selectOne")
    public RespBean selectOneById(@RequestParam("activityId") Integer activityId) {
        Activity activity = activityService.selectActivityById(activityId);
        return RespBean.success(activity);
    }
    
    @GetMapping("selectGoods")
    public RespBean selectGoodsByActivityId(@RequestParam("activityId") Integer activityId, @AuthenticationPrincipal MyUserDetails userDetails) {
        List<Goods> allGoodsList = activityService.selectGoodsByActivityId(activityId);
        if (userDetails == null) {
            return RespBean.error(RespBeanEnum.TOKEN_EXPIRED_ERROR);
        }
        if (userDetails.getStatus() == 1) {
            return RespBean.success(allGoodsList);
        } else {
            List<Goods> depositGoodsList = new ArrayList<>();
            for (Goods goods : allGoodsList) {
                if (goods.getType() == 0) {
                    depositGoodsList.add(goods);
                }
            }
            return RespBean.success(depositGoodsList);
        }
    }
}
